package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.Circle;
import cn.com.connext.scrm.repository.master.CircleRepository;
import cn.com.connext.scrm.service.dto.CircleDTO;
import cn.com.connext.scrm.service.mapper.CircleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by YANG205 on 2018/6/27.
 */
@Service
public class CircleService extends BaseService<Circle, String> {

    private final Logger log = LoggerFactory.getLogger(CircleService.class);

    private final CircleRepository circleRepository;

    @Autowired
    private CircleMapper circleMapper;

    public CircleService(CircleRepository circleRepository) {
        super(circleRepository);
        this.circleRepository = circleRepository;
    }

    public CircleDTO saveCircle(Circle entity) {
        log.debug("Request to save {} : {}", getEntityName(), entity);
        entity = circleRepository.save(entity);
        return circleMapper.toDto(entity);
    }

    /**
     * Get one circle by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    public CircleDTO findOneDTO(String id) {
        log.debug("Request to get Circle : {}", id);
        Circle circle = circleRepository.findOne(id);
        if (null != circle) {
            return circleMapper.toDto(circle);
        }
        return null;
    }

    /**
     * 删除圈子
     *
     * @param id
     * @return
     */
    public void deleteCircle(String id) {
        log.debug("Request to delete Circle : {}", id);
        Circle circle = circleRepository.findOne(id);
        if (null != circle) {
            circleRepository.delete(circle);
        }
    }

    public Specification<Circle> getWhereClause(final CircleDTO circleDTO) {
        Specification<Circle> specification = (Root<Circle> root, CriteriaQuery<?> query,
                                               CriteriaBuilder cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            query.orderBy(cb.desc(root.get("createTime").as(ZonedDateTime.class)));
            Predicate[] ps = new Predicate[predicates.size()];
            return cb.and(predicates.toArray(ps));
        };
        return specification;
    }

    public Page<CircleDTO> getAllCircles(Pageable pageable, CircleDTO circleDTO) {
        if (null == circleDTO) {
            circleDTO = new CircleDTO();
        }
        Page<CircleDTO> result = circleRepository.findAll(getWhereClause(circleDTO), pageable);
        return result;
    }

//    public Boolean reviewCircle(String id){
//
//    }
}
